package development.bean;

import java.util.Vector;

import state.GameForm;
import state.socket.DataPackage;
import state.socket.DataReceiveListener;
import state.socket.MapDataPackage;
import development.game.FrmAiLaTrieuPhu;
import development.game.FrmBaCay;
import development.game.FrmBroadChess;
import development.game.FrmCaro;
import development.game.FrmChess;
import development.game.FrmDaoVang;
import development.game.FrmTLMB;
import development.game.FrmTLMN;
import development.game.FrmTala;
import development.game.FrmXiTo;
import development.socket.ProtocolConstants;

public class Game extends Entity {
  private Vector[] roomByType = new Vector[3];
  
  public Game(String id1) {
    super(id1);
    for (int i = 0; i < 3; i++) {
      roomByType[i] = new Vector();
    }
  }

  public void putChild(Entity entity) {
    super.putChild(entity);
    Room room = (Room) entity;
    roomByType[room.getType()].addElement(room);
  }

  public Vector getRoomListByType(int type) {
    return roomByType[type];
  }
  
  public Room getChildByType(int type, int index) {
    return (Room) roomByType[type].elementAt(index);
  }

  public void clearChildList() {
    super.clearChildList();
    for (int i = 0; i < 3; i++) {
      roomByType[i].removeAllElements();
    }
  }

  public void onRecieveData(DataPackage dataPackage) {
    DataReceiveListener listener = (DataReceiveListener) getGameForm(id);
    listener.onRecieveData(dataPackage);
  }
  
  public static DataPackage getActionPackage(DataPackage dataPackage) {
    dataPackage.nextString();
    dataPackage.nextInt();
    dataPackage.setHeader(dataPackage.nextInt()); // Lấy data thứ 2 làm header
    return dataPackage;
  }

  public static final DataPackage createGamePackage(int header) {
    DataPackage dataPackage = DataPackage.createDataPackage1(ProtocolConstants.RequestHeader.GAME_ACTION_REQUEST);
    dataPackage.putString("header");
    dataPackage.putInt(MapDataPackage.INT_TYPE);
    dataPackage.putInt(header);
    return dataPackage;
  }

  public GameForm getGameForm() {
    return getGameForm(id);
  }
  
  public static GameForm getGameForm(String gameId) {
    if ("daovang".equals(gameId)) {
      return FrmDaoVang.instance();
    }
    
    if ("ailatrieuphu".equals(gameId)) {
      return FrmAiLaTrieuPhu.instance();
    }
    
    if ("audition".equals(gameId)) {
      // TODO: disable audition
//      if (Constants.IS_240x320_SCREEN) {
//        return FrmAudition.instance();
//      } else {
//        return FrmAudition320x240.instance();
//      }
    }
    
    if ("duoihinhbatchu".equals(gameId)) {
      return null;
      //return FrmDuoiHinhBatChu.instance();
    }
    
    if ("tlmn".equals(gameId)) {
      return FrmTLMN.instance();
    }
    
    if ("xito".equals(gameId)) {
      return FrmXiTo.instance();
    }
    
    if ("tala".equals(gameId)) {
      return FrmTala.instance();
    }
    
    if ("bacay".equals(gameId)) {
      return FrmBaCay.instance();
    }
    
    if ("tlmb".equals(gameId)) {
      return FrmTLMB.instance();
    }
    
    if ("cotuong".equals(gameId)) {
      return FrmChess.instance();
    }
    
    if ("caro".equals(gameId)) {
      return FrmCaro.instance();
    }
    
    if ("covua".equals(gameId)) {
      return FrmBroadChess.instance();
    }
    return null;
  }
}
